package com.jeesite.modules.task.dict.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.modules.annotation.RepeatSubmit;
import com.jeesite.modules.sys.utils.DictUtils;
import com.jeesite.modules.utils.Dict;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.task.dict.entity.DcSupTaskInfoDict;
import com.jeesite.modules.task.dict.service.DcSupTaskInfoDictService;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 任务部署信息表-任务类型Controller
 * @author zrr
 * @version 2022-03-11
 */
@Controller
@RequestMapping(value = "${adminPath}/dict/dcSupTaskInfoDict")
public class DcSupTaskInfoDictController extends BaseController {

	@Autowired
	private DcSupTaskInfoDictService dcSupTaskInfoDictService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public DcSupTaskInfoDict get(String id, boolean isNewRecord) {
		return dcSupTaskInfoDictService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequestMapping(value = {"list", ""})
	public String list(DcSupTaskInfoDict dcSupTaskInfoDict, Model model) {
		model.addAttribute("dcSupTaskInfoDict", dcSupTaskInfoDict);
		return "modules/task/dict/dcSupTaskInfoDictList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<DcSupTaskInfoDict> listData(DcSupTaskInfoDict dcSupTaskInfoDict, HttpServletRequest request, HttpServletResponse response) {

		dcSupTaskInfoDict.setPage(new Page<>(request, response));
		Page<DcSupTaskInfoDict> page = dcSupTaskInfoDictService.findPage(dcSupTaskInfoDict);
		if(dcSupTaskInfoDict.getPageNo()>1&&page.getList().size()==0){
			dcSupTaskInfoDict.setPageNo(1);
			page = dcSupTaskInfoDictService.findPage(dcSupTaskInfoDict);
		}
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequestMapping(value = "form")
	public String form(DcSupTaskInfoDict dcSupTaskInfoDict, Model model) {
		model.addAttribute("dcSupTaskInfoDict", dcSupTaskInfoDict);
		return "modules/task/dict/dcSupTaskInfoDictForm";
	}

	/**
	 * 保存数据
	 */

	@PostMapping(value = "save")
	@ResponseBody
	@RepeatSubmit
	public String save(@Validated DcSupTaskInfoDict dcSupTaskInfoDict) {
		dcSupTaskInfoDictService.save(dcSupTaskInfoDict);
		return renderResult(Global.TRUE, text("保存任务部署信息-任务类型成功！"));
	}
	
	/**
	 * 停用数据
	 */
	@RequestMapping(value = "disable")
	@ResponseBody
	@RepeatSubmit
	public String disable(DcSupTaskInfoDict dcSupTaskInfoDict) {
		dcSupTaskInfoDict.setDictStatus(DcSupTaskInfoDict.STATUS_DISABLE);
		dcSupTaskInfoDictService.update(dcSupTaskInfoDict);
		return renderResult(Global.TRUE, text("停用任务部署信息-任务类型成功"));
	}
	
	/**
	 * 启用数据
	 */
	@RequestMapping(value = "enable")
	@ResponseBody
	@RepeatSubmit
	public String enable(DcSupTaskInfoDict dcSupTaskInfoDict) {
		dcSupTaskInfoDict.setDictStatus(DcSupTaskInfoDict.STATUS_NORMAL);
		dcSupTaskInfoDictService.update(dcSupTaskInfoDict);
		return renderResult(Global.TRUE, text("启用任务部署信息-任务类型成功"));
	}
	
	/**
	 * 删除数据
	 */
	@RequestMapping(value = "delete")
	@ResponseBody
	@RepeatSubmit
	public String delete(DcSupTaskInfoDict dcSupTaskInfoDict) {
		dcSupTaskInfoDictService.delete(dcSupTaskInfoDict);
		return renderResult(Global.TRUE, text("删除任务部署信息-任务类型成功！"));
	}
	@RequestMapping(value = "queryDictList")
	@ResponseBody
	public String queryDictList(DcSupTaskInfoDict dcSupTaskInfoDict) {
		return renderResult(Global.TRUE, text("查询成功！"),dcSupTaskInfoDictService.queryDictList(dcSupTaskInfoDict));
	}
	@RequestMapping(value = "queryYearList")
	@ResponseBody
	public String queryYearList() {
		List<DictMap> list = new ArrayList<>();
		Date date = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		Integer year = calendar.get(Calendar.YEAR);
		DictMap dict = new DictMap();
		dict.setDictLabel(year);
		dict.setDictValue(year);


		DictMap dict_1 = new DictMap();
		dict_1.setDictLabel(year-1);
		dict_1.setDictValue(year-1);

		DictMap dict_2 = new DictMap();
		dict_2.setDictLabel(year-2);
		dict_2.setDictValue(year-2);

		list.add(dict);
		list.add(dict_1);
		list.add(dict_2);
		return renderResult(Global.TRUE, text("查询成功！"),list);
	}
	private class DictMap{
		private Integer DictLabel;
		private Integer dictValue;

		public Integer getDictLabel() {
			return DictLabel;
		}

		public void setDictLabel(Integer DictLabel) {
			this.DictLabel = DictLabel;
		}

		public Integer getDictValue() {
			return dictValue;
		}

		public void setDictValue(Integer dictValue) {
			this.dictValue = dictValue;
		}
	}
}